package com.kaibai.controller;

import com.kaibai.controller.BaseServlet;
import com.kaibai.pojo.Good;
import com.kaibai.pojo.Page;
import com.kaibai.service.GoodService;
import com.kaibai.service.impl.GoodServiceImpl;
import com.kaibai.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientGoodServlet extends BaseServlet {
    private GoodService goodService = new GoodServiceImpl();
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取参数
        int pageNo = WebUtil.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtil.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用service
        Page<Good> page = goodService.page(pageNo,pageSize);

        page.setUrl("good?action=page");
        //3.保存在request域中
        request.setAttribute("page",page);
        request.getRequestDispatcher("/jsp/client/index.jsp").forward(request,response);
    }
    protected void pageByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取参数
        int pageNo = WebUtil.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtil.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        int min = WebUtil.parseInt(request.getParameter("min"),0);
        int max = WebUtil.parseInt(request.getParameter("max"),Integer.MAX_VALUE);
        //2.调用service
        Page<Good> page = goodService.pageByPrice(pageNo,pageSize,min,max);
        String url = "good?action=pageByPrice&min="+min+"&max="+max;
        page.setUrl(url);
        //3.保存在request域中
        request.setAttribute("page",page);
        request.getRequestDispatcher("/jsp/client/index.jsp").forward(request,response);
    }

    protected void pageByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = WebUtil.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtil.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = request.getParameter("name");
        Page<Good> page = goodService.pageByName(pageNo,pageSize,name);
        String url = "good?action=pageByName&name="+name;
        page.setUrl(url);
        request.setAttribute("page",page);
        request.getRequestDispatcher("/jsp/client/index.jsp").forward(request,response);
    }

    protected void pageBySearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = WebUtil.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtil.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = request.getParameter("name").trim();
        int min = WebUtil.parseInt(request.getParameter("min"),0);
        int max = WebUtil.parseInt(request.getParameter("max"),Integer.MAX_VALUE);
        Page<Good> page = goodService.pageBySearch(pageNo,pageSize,name,min,max);
        String url = "good?action=pageBySearch&name="+name+"&min="+min+"&max="+max;
        page.setUrl(url);
        request.setAttribute("page",page);
        request.getRequestDispatcher("/jsp/client/index.jsp").forward(request,response);
    }


}
